﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrainingWorkshop.BLL;
using TrainingWorkshop.Model;

namespace Presentation
{
    public partial class Create : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Logical log = new Logical();
                log.ChooseTable("Department", true);
                TeacherNameBox.Text = "Please Type into Name of Teacher";
                
                DropDownListDepartment.DataSource = log.Dep;
                //DropDownListDepartment.DataValueField = "DepartmentId";
                //DropDownListDepartment.DataTextField = "DepartmentName";
                //DropDownListDepartment.DataBind();
                DropDownListDepartment.Items.Clear();//Because dropdownlist is bond to log.dep, so need clear last item set,otherwise it's duplicate   
                DropDownListDepartment.Items.Capacity=log.Dep.Count;
                DropDownListDepartment.Items.Insert(0,"Please Select");
                foreach (var a in log.Dep)
                {
                    ListItem b = new ListItem(a.DepartmentName);
                    if (!DropDownListDepartment.Items.Contains(b))
                    {
                        DropDownListDepartment.Items.Insert(a.DepartmentId, a.DepartmentName);
                        
                    }

                }
            }
        }

        protected void Button_Create_Click(object sender, EventArgs e) //return teacher list page
        {
     
            Logical log=new Logical();
                        
            string Gender = null;
            
            int num = DropDownListDepartment.SelectedIndex;
            if (0 == RadioButtonList1.SelectedIndex)
            { 
                Gender = "Male"; 
            }
            else
            {
                Gender = "Female";
            }

           

            Department mydep = new Department();
            Logical l = new Logical();
            l.ChooseTable("Department", false, num);
            foreach (var p in l.Dep)
            {
                mydep = p;
            }

            Teacher par = new Teacher(0, TeacherNameBox.Text, Gender, mydep);


            if (log.OperationChoose("Insert",false,par))
               Response.Write("<script>alert('Insert Succeed');window.location.href='TeacherList.aspx';</script>");
            
            else
                Response.Write("<script>alert('Insert Failed');window.location.href='TeacherList.aspx';</script>");
            
            Response.End();//将当前所有缓冲的输出发送到客户端，停止该页的执行，详见函数说明

            
        }
    }
}